//
//  XLCardsVucherCell.m
//  XiangLove
//
//  Created by 宋振 on 2018/8/28.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLCardsVucherCell.h"

#import "XLPersonCenterTool.h"

@interface XLCardsVucherCell ()

//卡片堆砌个数
@property (nonatomic, strong) UILabel *  cardsCountLbl;

//卡券种类
@property (nonatomic, strong) UILabel *  cardsStyleLbl;

//所属礼包种类
@property (nonatomic, strong) UILabel *  bagStyleLbl;

//免费时间
@property (nonatomic, strong) UILabel *  freeTimeLbl;

//免费时间说明
@property (nonatomic, strong) UILabel *  instroctionLbl;

//有效期
@property (nonatomic, strong) UILabel *  timesLbl;

@end

@implementation XLCardsVucherCell
{
    //背景板
    UIImageView *_backgouroundImageView;
    
    //卡券个数背景板
    UIView *cardsCountView;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        /**
         设置UI
         */
        [self setCustomeView];
        
    }
    
    return self;
}

/**
 设置UI
 */
- (void)setCustomeView
{
    
    //设置背景卡片
    _backgouroundImageView = [[UIImageView alloc] init];
    _backgouroundImageView.image = [[UIImage imageNamed:@"pic_kaquan_n"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    _backgouroundImageView.layer.masksToBounds = YES;
    _backgouroundImageView.layer.cornerRadius = 12;
    
//    [self addShadowToView:_backgouroundImageView withColor:RGBA(0, 0, 0, 0.06)];
    
    CALayer *subLayer = [CALayer layer];
    
    subLayer.shadowOffset = CGSizeMake(1, 5);
    
    subLayer.masksToBounds = NO;
    
    subLayer.shadowRadius = 5;
    
    subLayer.shadowColor = RGBA(0, 0, 0, 0.06).CGColor;
    
    subLayer.shadowOpacity = 0.9;
    
    [_backgouroundImageView.layer insertSublayer:subLayer below:_backgouroundImageView.layer];
    
    [self.contentView addSubview:_backgouroundImageView];
    
    [_backgouroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(10);
        make.left.equalTo(self.contentView.mas_left).offset(kScale(15));
        make.right.equalTo(self.contentView.mas_right).offset(-kScale(15));
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-2);
    }];
    
    //卡券堆砌个数背景
    cardsCountView = [self setCardsCountBackgroundView];
    [self.contentView addSubview:cardsCountView];
    
    //裁剪三个圆角
    cardsCountView.layer.mask = [self clipCornerWithView:cardsCountView roundCorners:UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(2, 2)];
    
    //卡片堆砌个数
    [self.contentView addSubview:self.cardsCountLbl];
    
    [self.cardsCountLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(cardsCountView).insets(UIEdgeInsetsMake(0, 1, 0, 0));
    }];
    
    //免费视频聊天卡（卡券种类）
    [self.contentView addSubview:self.cardsStyleLbl];
    
    [self.cardsStyleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_backgouroundImageView.mas_top).offset(18);
        make.left.equalTo(_backgouroundImageView.mas_left).offset(24);
        make.height.mas_equalTo(17);
    }];
    
    //所属礼包种类
    [self.contentView addSubview:self.bagStyleLbl];
    
    [self.bagStyleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cardsCountView.mas_bottom).offset(1);
        make.right.equalTo(_backgouroundImageView.mas_right).offset(-kScale(24));
        make.height.mas_equalTo(15);
    }];
    
    //装饰
    UIImageView *libaoImage = [[UIImageView alloc] init];
    libaoImage.image = [UIImage imageNamed:@"pic_kaquanlibao_n"];
    libaoImage.hidden = YES;
    [self.contentView addSubview:libaoImage];
    
    [libaoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bagStyleLbl.mas_centerY);
        make.right.equalTo(self.bagStyleLbl.mas_left).offset(-4);
        make.size.mas_offset(CGSizeMake(16, 16));
    }];
    
    //免费时间
    [self.contentView addSubview:self.freeTimeLbl];
    
    [self.freeTimeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_backgouroundImageView.mas_top).offset(65);
        make.centerX.equalTo(_backgouroundImageView.mas_centerX);
        make.height.mas_offset(20);
    }];
    
    //免费卡券说明
    [self.contentView addSubview:self.instroctionLbl];
    
    [self.instroctionLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.freeTimeLbl.mas_bottom).offset(17);
        make.left.equalTo(_backgouroundImageView.mas_left).offset(kScale(15));
        make.right.equalTo(_backgouroundImageView.mas_right).offset(-kScale(15));
        make.height.mas_offset(12);
    }];
    
    //时间有效期背景
    UIView *timebackground = [[UIView alloc] init];
    timebackground.backgroundColor = RGB(62, 184, 105);
    [_backgouroundImageView addSubview:timebackground];
    
    [timebackground mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.and.bottom.equalTo(_backgouroundImageView);
        make.top.equalTo(self.instroctionLbl.mas_bottom).offset(26);
    }];
    
    
    //时间有效期
    [self.contentView addSubview:self.timesLbl];
    
    [self.timesLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_backgouroundImageView.mas_left).offset(kScale(24));
        make.bottom.equalTo(_backgouroundImageView.mas_bottom).offset(-8);
        make.height.mas_equalTo(17);
    }];
    
}

//设置卡包个数背景
- (UIView *)setCardsCountBackgroundView
{
    UIView *backgroundView = [[UIView alloc] init];
    backgroundView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:backgroundView];
    
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_backgouroundImageView.mas_top).offset(-1);
        make.right.equalTo(_backgouroundImageView.mas_right).offset(-kScale(7));
        make.size.mas_equalTo(CGSizeMake(37, 22));
    }];
    
    UIView *backgroundOne = [[UIView alloc] init];
    backgroundOne.backgroundColor = RGB(255, 187, 1);
    [backgroundView addSubview:backgroundOne];
    
    [backgroundOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.right.equalTo(backgroundView);
        make.height.mas_offset(1);
    }];
    
    UIView *backgroundTwo = [[UIView alloc] init];
    backgroundTwo.backgroundColor = RGB(255, 187, 1);
    [backgroundView addSubview:backgroundTwo];
    
    [backgroundTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.and.right.equalTo(backgroundView);
        make.left.equalTo(backgroundView.mas_left).offset(1);
    }];
    
    [self layoutIfNeeded];
    
    return backgroundView;
}





//卡券堆砌个数
- (UILabel *)cardsCountLbl
{
    if (!_cardsCountLbl) {
        self.cardsCountLbl = [[UILabel alloc] init];
        self.cardsCountLbl.textColor = [UIColor whiteColor];
        self.cardsCountLbl.textAlignment = NSTextAlignmentCenter;
        self.cardsCountLbl.font = [UIFont fontWithName:kFontNamePFSemibold size:12];
        self.cardsCountLbl.text = @"2哈";
        self.cardsCountLbl.hidden = NO;
    }
    
    return _cardsCountLbl;
}

//免费视频聊天卡
- (UILabel *)cardsStyleLbl{
    
    if (!_cardsStyleLbl) {
        self.cardsStyleLbl = [[UILabel alloc] init];
        self.cardsStyleLbl.textAlignment = NSTextAlignmentLeft;
        self.cardsStyleLbl.attributedText = [self setCardsStyleWithString:@"免费视频聊天卡呀"];
    }
    
    return _cardsStyleLbl;
}

//所属礼包种类
- (UILabel *)bagStyleLbl{
    
    if (!_bagStyleLbl) {
        self.bagStyleLbl = [[UILabel alloc] init];
        self.bagStyleLbl.attributedText = [self setBagStyleWithString:@""];
        self.bagStyleLbl.textAlignment = NSTextAlignmentRight;
        self.bagStyleLbl.hidden = YES;
    }
    return _bagStyleLbl;
}

//免费时间显示
- (UILabel *)freeTimeLbl
{
    if (!_freeTimeLbl) {
        self.freeTimeLbl = [[UILabel alloc] init];
        self.freeTimeLbl.textAlignment = NSTextAlignmentCenter;
        self.freeTimeLbl.attributedText = [self setFreeTimeWithString:@"1钟分"];
    }
    
    return _freeTimeLbl;
}

//免费时间说明
- (UILabel *)instroctionLbl{
    
    if (!_instroctionLbl) {
        self.instroctionLbl = [[UILabel alloc] init];
        self.instroctionLbl.attributedText = [self setInstroctionOrTimesWithString:@"说明七分钟免费视频聊天"];
        self.instroctionLbl.textAlignment = NSTextAlignmentCenter;
    }
    return _instroctionLbl;
}

//时间有效期显示
- (UILabel *)timesLbl
{
    if (!_timesLbl) {
        self.timesLbl = [[UILabel alloc] init];
        self.timesLbl.attributedText = [self setInstroctionOrTimesWithString:@"有效期时间"];
        self.timesLbl.textAlignment = NSTextAlignmentLeft;
    }
    
    return _timesLbl;
}

#pragma mark    - - - Toos - - -

/**
 卡券种类文字修饰
 */
- (NSAttributedString *)setCardsStyleWithString:(NSString *)string
{
    NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:string attributes:@{
                                                                                                   NSKernAttributeName:@2,
                                                                                                   NSForegroundColorAttributeName:RGB(255, 255, 255),
                                                                                                   NSFontAttributeName:[UIFont fontWithName:kFontNamePFRegular size:17]
                                                                                                   }];
    
    return attribute;
}

//所属礼包种类文字修饰
- (NSAttributedString *)setBagStyleWithString:(NSString *)string
{
    NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:string attributes:@{
                                                                                                   NSKernAttributeName:@2,
                                                                                                   NSForegroundColorAttributeName:RGB(255, 255, 255),
                                                                                                   NSFontAttributeName:[UIFont fontWithName:kFontNamePFRegular size:12]
                                                                                                   }];
    
    return attribute;
}

//免费时间字体修饰
- (NSAttributedString *)setFreeTimeWithString:(NSString *)string
{
    //给字体设置阴影
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowBlurRadius = 4.0;
    shadow.shadowColor = RGBA(0, 0, 0, 0.12);
    shadow.shadowOffset = CGSizeMake(0, 2);
    
    NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:string attributes:@{
                                                                                                   NSKernAttributeName:@3,
                                                                                                   NSForegroundColorAttributeName:RGB(255, 255, 255),
                                                                                                   NSFontAttributeName:[UIFont fontWithName:kFontNamePFSemibold size:24],
                                                                                                   NSShadowAttributeName:shadow
                                                                                                   }];
    
    return attribute;
}

//卡券说明 有效期时间字体修饰
- (NSAttributedString *)setInstroctionOrTimesWithString:(NSString *)string
{
    NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:string attributes:@{
                                                                                                   NSKernAttributeName:@1,
                                                                                                   NSForegroundColorAttributeName:RGB(255, 255, 255),
                                                                                                   NSFontAttributeName:[UIFont fontWithName:kFontNamePFRegular size:12]
                                                                                                   }];
    
    return attribute;
}

/// 添加单边阴影效果
- (void)addShadowToView:(UIView *)theView withColor:(UIColor *)theColor {
    theView.layer.shadowColor = theColor.CGColor;
    theView.layer.shadowOffset = CGSizeMake(2,2);
    theView.layer.shadowOpacity = 1;
    theView.layer.shadowRadius = 4;
    // 单边阴影 顶边
    float shadowPathWidth = theView.layer.shadowRadius;
    CGRect shadowRect = CGRectMake(0, theView.bounds.size.height-3, theView.bounds.size.width, shadowPathWidth);
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:shadowRect];
    theView.layer.shadowPath = path.CGPath;
}

- (CAShapeLayer *)clipCornerWithView:(UIView *)contentView roundCorners:(UIRectCorner)rectCorner cornerRadii:(CGSize)cornerRad
{
    [self layoutIfNeeded];
    
    UIBezierPath *fieldPath = [UIBezierPath bezierPathWithRoundedRect:contentView.bounds byRoundingCorners:rectCorner cornerRadii:cornerRad];
    
    CAShapeLayer *fieldLayer = [[CAShapeLayer alloc] init];
    
    fieldLayer.frame = contentView.bounds;
    
    fieldLayer.backgroundColor = RGB(62, 184, 105).CGColor;
    
    fieldLayer.path = fieldPath.CGPath;
    
    return fieldLayer;
}


#pragma mark    - - - 设置数据 - - -

- (void)setModel:(XLCardsVucherModel *)model
{
    _model = model;
    if (model) {
        
        
        //卡券堆砌个数显示
        if (model.CountInteger > 1) {
            self.cardsCountLbl.hidden = NO;
            cardsCountView.hidden = NO;
            self.cardsCountLbl.text = [NSString stringWithFormat:@"%ld张",model.CountInteger];
        }else{
            self.cardsCountLbl.hidden = YES;
            cardsCountView.hidden = YES;
        }
        
        //标题显示
        self.cardsStyleLbl.attributedText = [self setCardsStyleWithString:K_DEFULT(model.Title)];
        
        //时间显示
        NSString *time;
        
        if (model.ChatInteger/60 >= 1) {
            
            if (model.ChatInteger%60 > 0) {
                time = [NSString stringWithFormat:@"%ld分钟%ld秒",model.ChatInteger/60,model.ChatInteger%60];
            }else{
                time = [NSString stringWithFormat:@"%ld分钟",model.ChatInteger/60];
            }
        }else{
            time = [NSString stringWithFormat:@"%ld秒",model.ChatInteger];
        }
        
        self.freeTimeLbl.attributedText = [self setFreeTimeWithString:time];
        
        //说明
        self.instroctionLbl.attributedText = [self setInstroctionOrTimesWithString:[NSString stringWithFormat:@"可享受%@免费视频通话",time]];
        
        //有效期
        self.timesLbl.attributedText = [self setInstroctionOrTimesWithString:[NSString stringWithFormat:@"有效期：%@-%@",K_DEFULT(model.CreateDate),K_DEFULT(model.ExpiryDate)]];
        
    }
}


@end
